package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(tags = "购物车相关接口")
@RestController
@RequestMapping("/user/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @ApiOperation("添加购物车")
    @PostMapping("/add")
    public Result addCart(@RequestBody ShoppingCartDTO dto) {
        log.info("添加购物车:{}",dto);
        shoppingCartService.addCart(dto);
        return Result.success();
    }

    @ApiOperation("查看购物车")
    @GetMapping("/list")
    public Result list() {
       return Result.success(shoppingCartService.list());
    }

    @ApiOperation("清空购物车")
    @DeleteMapping("/clean")
    public Result clean() {
        shoppingCartService.clean();
        return Result.success();
    }

    @ApiOperation("删除购物车中一个商品")
    @PostMapping("/sub")
    public Result subCart(@RequestBody ShoppingCartDTO dto) {
        log.info("删除购物车中一个商品:{}",dto);
        ShoppingCart shoppingCart = ShoppingCart.builder().build();
        BeanUtils.copyProperties(dto, shoppingCart);
        shoppingCartService.subCart(shoppingCart);
        return Result.success();
    }
}
